SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
C

C static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your C code

  • All rules 315
  • Vulnerability13
  • Bug76
  • Security Hotspot19
  • Code Smell207

  • Quick Fix 19
 
Tags
    Impact
      Clean code attribute
        1. Hard-coded secrets are security-sensitive

           Security Hotspot
        2. "sprintf" should not be used

           Security Hotspot
        3. Changing working directories without verifying the success is security-sensitive

           Security Hotspot
        4. Setting capabilities is security-sensitive

           Security Hotspot
        5. Using "tmpnam", "tmpnam_s" or "tmpnam_r" is security-sensitive

           Security Hotspot
        6. Using "strncpy" or "wcsncpy" is security-sensitive

           Security Hotspot
        7. Using "strncat" or "wcsncat" is security-sensitive

           Security Hotspot
        8. Using "strcat" or "wcscat" is security-sensitive

           Security Hotspot
        9. Using "strlen" or "wcslen" is security-sensitive

           Security Hotspot
        10. Changing directories improperly when using "chroot" is security-sensitive

           Security Hotspot
        11. Using "strcpy" or "wcscpy" is security-sensitive

           Security Hotspot
        12. Using publicly writable directories is security-sensitive

           Security Hotspot
        13. Using clear-text protocols is security-sensitive

           Security Hotspot
        14. Expanding archive files without controlling resource consumption is security-sensitive

           Security Hotspot
        15. Using weak hashing algorithms is security-sensitive

           Security Hotspot
        16. Setting loose POSIX file permissions is security-sensitive

           Security Hotspot
        17. Using pseudorandom number generators (PRNGs) is security-sensitive

           Security Hotspot
        18. Hard-coded passwords are security-sensitive

           Security Hotspot
        19. Using hardcoded IP addresses is security-sensitive

           Security Hotspot

        Expanding archive files without controlling resource consumption is security-sensitive

        intentionality - complete
        security
        Security Hotspot
        • cwe
        • cert

        Successful Zip Bomb attacks occur when an application expands untrusted archive files without controlling the size of the expanded data, which can lead to denial of service. A Zip bomb is usually a malicious archive file of a few kilobytes of compressed data but turned into gigabytes of uncompressed data. To achieve this extreme compression ratio, attackers will compress irrelevant data (eg: a long string of repeated bytes).

        Ask Yourself Whether

        Archives to expand are untrusted and:

        • There is no validation of the number of entries in the archive.
        • There is no validation of the total size of the uncompressed data.
        • There is no validation of the ratio between the compressed and uncompressed archive entry.

        There is a risk if you answered yes to any of those questions.

        Recommended Secure Coding Practices

        • Define and control the threshold for maximum total size of the uncompressed data.
        • Count the number of file entries extracted from the archive and abort the extraction if their number is greater than a predefined threshold, in particular it’s not recommended to recursively expand archives (an entry of an archive could be also an archive).

        Sensitive Code Example

        #include <archive.h>
        #include <archive_entry.h>
        // ...
        
        void f(const char *filename, int flags) {
          struct archive_entry *entry;
          struct archive *a = archive_read_new();
          struct archive *ext = archive_write_disk_new();
          archive_write_disk_set_options(ext, flags);
          archive_read_support_format_tar(a);
        
          if ((archive_read_open_filename(a, filename, 10240))) {
            return;
          }
        
          for (;;) {
            int r = archive_read_next_header(a, &entry);
            if (r == ARCHIVE_EOF) {
              break;
            }
            if (r != ARCHIVE_OK) {
              return;
            }
          }
          archive_read_close(a);
          archive_read_free(a);
        
          archive_write_close(ext);
          archive_write_free(ext);
        }
        

        Compliant Solution

        #include <archive.h>
        #include <archive_entry.h>
        // ...
        
        int f(const char *filename, int flags) {
          const int max_number_of_extraced_entries = 1000;
          const int64_t max_file_size = 1000000000; // 1 GB
        
          int number_of_extraced_entries = 0;
          int64_t total_file_size = 0;
        
          struct archive_entry *entry;
          struct archive *a = archive_read_new();
          struct archive *ext = archive_write_disk_new();
          archive_write_disk_set_options(ext, flags);
          archive_read_support_format_tar(a);
          int status = 0;
        
          if ((archive_read_open_filename(a, filename, 10240))) {
            return -1;
          }
        
          for (;;) {
            number_of_extraced_entries++;
            if (number_of_extraced_entries > max_number_of_extraced_entries) {
              status = 1;
              break;
            }
        
            int r = archive_read_next_header(a, &entry);
            if (r == ARCHIVE_EOF) {
              break;
            }
            if (r != ARCHIVE_OK) {
              status = -1;
              break;
            }
        
            int file_size = archive_entry_size(entry);
            total_file_size += file_size;
            if (total_file_size > max_file_size) {
              status = 1;
              break;
            }
          }
          archive_read_close(a);
          archive_read_free(a);
        
          archive_write_close(ext);
          archive_write_free(ext);
        
          return status;
        }
        

        See

        • OWASP - Top 10 2021 Category A1 - Broken Access Control
        • OWASP - Top 10 2021 Category A5 - Security Misconfiguration
        • OWASP - Top 10 2017 Category A5 - Broken Access Control
        • OWASP - Top 10 2017 Category A6 - Security Misconfiguration
        • CWE - CWE-409 - Improper Handling of Highly Compressed Data (Data Amplification)
        • bamsoftware.com - A better Zip Bomb
          Available In:
        • SonarQube IdeCatch issues on the fly,
          in your IDE
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories
        • SonarQube ServerAnalyze code in your
          on-premise CI
          Developer Edition
          Available Since
          9.1

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use